/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.air.web;

import cn.mcsys.myadmin.common.config.Global;
import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.utils.DateUtils;
import cn.mcsys.myadmin.common.web.BaseController;
import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.modules.air.entity.AirRealtimeMessage;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.air.service.AirRealtimeMessageService;
import cn.mcsys.myadmin.modules.air.service.AirRidePointService;
import cn.mcsys.myadmin.modules.air.vo.AirRealMessageVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 实习消息Controller
 * @author leopold
 * @version 2018-08-13
 */
@Controller
@RequestMapping(value = "${adminPath}/air/airRealtimeMessage")
public class AirRealtimeMessageController extends BaseController {

	@Autowired
	private AirRealtimeMessageService airRealtimeMessageService;

	@Autowired
	private AirRidePointService airRidePointService;
	
	@RequiresPermissions("air:airRealtimeMessage:view")
	@RequestMapping(value = {"list", ""})
	public String list(AirRealtimeMessage airRealtimeMessage, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AirRealtimeMessage> page = airRealtimeMessageService.findPage(new Page<AirRealtimeMessage>(request, response), airRealtimeMessage);
		List<AirRidePoint> ridePoints = airRidePointService.findAllList();
		model.addAttribute("rides",ridePoints);
		if(airRealtimeMessage.getFeedbackDate() == null){
			model.addAttribute("feedDate",new Date());
		}else{
			model.addAttribute("feedDate",airRealtimeMessage.getFeedbackDate());
		}
		model.addAttribute("page", page);
		return "modules/air/airRealtimeMessageList";
	}

	@RequiresPermissions("air:airRealtimeMessage:view")
	@RequestMapping(value = "form")
	public String form(AirRealtimeMessage airRealtimeMessage, Model model) {
	    if (airRealtimeMessage.getId() != null){
            airRealtimeMessage = airRealtimeMessageService.get(airRealtimeMessage.getId());
        }
		model.addAttribute("airRealtimeMessage", airRealtimeMessage);
		return "modules/air/airRealtimeMessageForm";
	}

	@RequiresPermissions("air:airRealtimeMessage:edit")
	@RequestMapping(value = "save")
	public String save(AirRealtimeMessage airRealtimeMessage, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, airRealtimeMessage)){
			return form(airRealtimeMessage, model);
		}
		airRealtimeMessageService.save(airRealtimeMessage);
		addMessage(redirectAttributes, "保存消息成功");
		return "redirect:"+Global.getAdminPath()+"/air/airRealtimeMessage/?repage";
	}
	
	@RequiresPermissions("air:airRealtimeMessage:edit")
	@RequestMapping(value = "delete")
	public String delete(AirRealtimeMessage airRealtimeMessage, RedirectAttributes redirectAttributes) {
		airRealtimeMessageService.delete(airRealtimeMessage);
		addMessage(redirectAttributes, "删除消息成功");
		return "redirect:"+Global.getAdminPath()+"/air/airRealtimeMessage/?repage";
	}

	@RequiresPermissions("air:airRealtimeMessage:view")
	@RequestMapping(value = "index")
	public String realMessageIndex(Model model){
//		查询所有的调度请求
		List<AirRealtimeMessage> carInsufficient = airRealtimeMessageService.selectMessage4type(AirRealtimeMessage.CAR_INSUFFICIENT, AirRealtimeMessage.LIMIT_THIRTY);
//		查询所有调度车辆
		List<AirRealtimeMessage> messages = airRealtimeMessageService.selectMessage4type(AirRealtimeMessage.SCHEDULING,AirRealtimeMessage.LIMIT_THIRTY);
//		查询所有的异常反馈
        List<AirRealtimeMessage> exceptions = airRealtimeMessageService.selectMessage4type(AirRealtimeMessage.ABNORMAL_FEEDBACK, AirRealtimeMessage.LIMIT_THIRTY);
//      查询所有的信息报送
		List<AirRealtimeMessage> departure = airRealtimeMessageService.selectMessage4type(AirRealtimeMessage.DEPARTURE, AirRealtimeMessage.LIMIT_THIRTY);
		AirRealMessageVO vo = airRealtimeMessageService.selectRealMessageTypeCount();
		vo.setSchedulingList(carInsufficient);
		vo.setAbnormalList(exceptions);
		vo.setOperationList(messages);
		vo.setSubmissionList(departure);
		model.addAttribute("vo",vo);
		return "modules/air/airRealMessageTotal";
	}

	@RequiresPermissions("air:airRealtimeMessage:view")
	@GetMapping(value = "getMessageTotal")
	@ResponseBody
	public JsonResult getMessageTotal(Model model) {
//		查询所有的调度请求
		List<AirRealtimeMessage> carInsufficient = airRealtimeMessageService.selectMessage4type(AirRealtimeMessage.CAR_INSUFFICIENT, AirRealtimeMessage.LIMIT_THIRTY);
//		查询所有调度车辆
		List<AirRealtimeMessage> messages = airRealtimeMessageService.selectMessage4type(AirRealtimeMessage.SCHEDULING, AirRealtimeMessage.LIMIT_THIRTY);
//		查询所有的异常反馈
		List<AirRealtimeMessage> exceptions = airRealtimeMessageService.selectMessage4type(AirRealtimeMessage.ABNORMAL_FEEDBACK, AirRealtimeMessage.LIMIT_THIRTY);
//      查询所有的信息报送
		List<AirRealtimeMessage> departure = airRealtimeMessageService.selectMessage4type(AirRealtimeMessage.DEPARTURE, AirRealtimeMessage.LIMIT_THIRTY);
		AirRealMessageVO vo = airRealtimeMessageService.selectRealMessageTypeCount();
		vo.setSchedulingList(carInsufficient);
		vo.setAbnormalList(exceptions);
		vo.setOperationList(messages);
		vo.setSubmissionList(departure);
		model.addAttribute("vo", vo);
		return JsonResult.success(vo);
	}

	/**
	 * 查询事故反馈或发车候车
	 * @return
	 */
	@GetMapping("getSubmission")
	@ResponseBody
	public JsonResult getSubmission(){
		return JsonResult.success(airRealtimeMessageService.getSubmission(DateUtils.formatDate(new Date(),"yyyy-MM-dd")));
	}

//	/**
//	 * 问题反馈(type = 1,2,3)、信息报送(type = 4,5)
//	 * @param airRidePointId 现场乘车点
//	 * @param airRealtimeMessage 实时消息
//	 * @param airWorker 工作人员名字
//	 * @param type 反馈问题或发车候车
//	 * @param title 反馈类型 type = 1,2,3 时才有
//	 * @param note 反馈问题或发车候车人数
//	 * @param carNumber 车牌号
//	 * @return
//	 */
//	@PostMapping("sendMessage")
//	public JsonResult sendMessage(Integer airRidePointId,AirRealtimeMessage airRealtimeMessage,String carNumber){
//		airRealtimeMessageService.sendMessage(airRidePointId,airRealtimeMessage,carNumber);
//		return JsonResult.success();
//	}


}